package com.surpassun.emall.model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.Index;
import org.hibernate.validator.constraints.Email;

import com.surpassun.emall.util.StringPool;

/**
 *
 * @author Xun Ren : sakyrenxun@gmail.com
 */
@Entity
public class User implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    
    @NotNull
    private Date createDate;
    
    private String title;

    @NotNull
    private String firstname;
    
    @NotNull
    private String lastname;
    
    @Transient
    private String fullname;
    
    @Index(name = "INDEX_USER_EMAIL")
    @NotNull
    @Email
    @Column(unique = true)
    private String email;
    
    @NotNull
    private String password;
    
    private String phone;
    
    private String lastLoginIP;
    
    private Date lastLoginDate;
    
    private boolean active;
    
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "address_FK")
    private Address address;
    
    @ManyToMany
    @JoinTable(name = "Users_Roles", joinColumns = @JoinColumn(name = "userId"), inverseJoinColumns = @JoinColumn(name = "roleId"))
    private List<Role> roles;
    
    @OneToMany(mappedBy = "owner")
    private List<Store> stores;

    public User() {
    }

    public User(String title, Date createDate, String firstname, String lastname, String email, String password,
    		String phone, boolean active, Address address) {
        this.title = title;
        this.createDate = createDate;
        this.firstname = firstname;
        this.lastname = lastname;
        this.email = email;
        this.password = password;
        this.phone = phone;
        this.active = active;
        this.address = address;
    }
    
    public User(String title, Date createDate, String firstname, String lastname, String email, String password,
    		String phone, String lastLoginIP, Date lastLoginDate, boolean active, Address address) {
        this.title = title;
        this.createDate = createDate;
        this.firstname = firstname;
        this.lastname = lastname;
        this.email = email;
        this.password = password;
        this.phone = phone;
        this.lastLoginIP = lastLoginIP;
        this.lastLoginDate = lastLoginDate;
        this.active = active;
        this.address = address;
    }
    
    public User(String title, Date createDate, String firstname, String lastname, String email, String password,
    		String phone, String lastLoginIP, Date lastLoginDate, boolean active, Address address, List<Role> roles) {
    	this.title = title;
    	this.createDate = createDate;
    	this.firstname = firstname;
    	this.lastname = lastname;
    	this.email = email;
    	this.password = password;
    	this.phone = phone;
    	this.lastLoginIP = lastLoginIP;
        this.lastLoginDate = lastLoginDate;
        this.active = active;
    	this.address = address;
    	this.roles = roles;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getFullname() {
		return firstname + StringPool.SPACE + lastname;
	}

	public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getLastLoginIP() {
		return lastLoginIP;
	}

	public void setLastLoginIP(String lastLoginIP) {
		this.lastLoginIP = lastLoginIP;
	}

	public Date getLastLoginDate() {
		return lastLoginDate;
	}

	public void setLastLoginDate(Date lastLoginDate) {
		this.lastLoginDate = lastLoginDate;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public List<Store> getStores() {
		return stores;
	}

	public void setStores(List<Store> stores) {
		this.stores = stores;
	}
}